package com.newland.esp.controller.user;

import com.newland.esp.common.util.DataResult;
import com.newland.esp.common.util.login.Result;
import com.newland.esp.entity.user.AdminInfoEntity;
import com.newland.esp.service.user.AdminInfoService;
import com.newland.esp.vo.response.user.AdminInfoSearchVO;
import com.newland.esp.vo.response.user.TableDataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @param
 * @author lw
 * @descripe
 * @date 2021/8/17 22:09
 * @Version 1.0
 **/
@Api(tags="用户列表显示")
@RestController
@CrossOrigin
@RequestMapping("/user")
public class AdminInfoController {

    @Autowired
    private AdminInfoService adminInfoService;

//    @ApiOperation("获取用户信息")
//    @RequestMapping("/list1")
//    public TableDataVO list(Integer page, Integer limit){
//        return adminInfoService.findAdminInfo(page, limit);
//    }
//    @InitBinder
//    public void initBinder(WebDataBinder binder, WebRequest request) {
//        //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
//        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
//    }

    @ApiOperation("获取用户信息")
    @RequestMapping("/list")
    public TableDataVO list(Integer page, Integer limit, AdminInfoSearchVO adminInfoSearchVO){
        return adminInfoService.findAdminInfo(page, limit,adminInfoSearchVO);
    }

    @ApiOperation("添加用户")
    @RequestMapping("/add")
    public DataResult add(@RequestBody AdminInfoEntity adminInfo){

        return  adminInfoService.addAdminInfo(adminInfo);
    }

    @ApiOperation("修改用户")
    @RequestMapping("/updata")
    public DataResult updata(@RequestBody AdminInfoEntity adminInfo){

        return  adminInfoService.updateAdminInfo(adminInfo);
    }

    @ApiOperation(value = "逻辑删除用户信息")
    @DeleteMapping("{id}")
    public DataResult remove(@PathVariable Short id) {
        boolean flag = adminInfoService.removeById(id);
        if(flag){
            return DataResult.success();
        }else {
            return DataResult.error();
        }
    }

    @ApiOperation(value = "批量逻辑删除用户信息")
    @DeleteMapping("/batchRemove")
    public DataResult batchRemoveUserSet(@RequestBody List<Short> idList) {
        adminInfoService.removeByIds(idList);
        return DataResult.success();
    }

    @ApiOperation(value = "设置用户锁定和解锁")
    @PutMapping("/lockUserSet/{id}/{status}")
    public DataResult lockUserSet(@PathVariable Short id,
                                  @PathVariable Short status) {
        //根据id查询用户设置信息
        AdminInfoEntity adminInfo = adminInfoService.getById(id);
        //设置状态
        adminInfo.setStatus(status);
        //调用方法
        adminInfoService.updateById(adminInfo);
        return DataResult.success();
    }

    @ApiOperation(value = "获取所有权限")
    @RequestMapping(value = "/getAllAuth",method = RequestMethod.GET)
    public DataResult getAllAuth(){
        return adminInfoService.GetAllAuth();
    }

    @ApiOperation(value = "设置用户的权限ID")
    @PutMapping("/setUserId/{id}/{roleId}")
    public DataResult setUserId(@PathVariable Short id,
                                @PathVariable Short roleId) {
        return adminInfoService.setUserIdByRoleId(id,roleId);
    }

    @ApiOperation(value = "根据userID获取RoleID")
    @RequestMapping(value = "/getRoleIdByuserId/{userId}",method = RequestMethod.GET)
    public DataResult getRoleIdByuserId(@PathVariable Short userId){
        return adminInfoService.getRoleIdByUserId(userId);
    }

    @ApiOperation(value = "获取所有用户权限列表展示")
    @RequestMapping(value = "/getAllUserAuth",method = RequestMethod.GET)
    public DataResult GetAllAuthInfo(){
        return adminInfoService.GetAllAuthInfo();
    }

    @ApiOperation(value = "获取用户权限树状图展示")
    @RequestMapping(value = "/getUserAuthChildrenList",method = RequestMethod.GET)
    public DataResult getUserAuthList(){
        return adminInfoService.getUserAuthList();
    }

    @ApiOperation(value="获取所有角色对应的权限子父级列表")
    @GetMapping("/getRoleAuthChildrenList")
    public DataResult getRoleAuthChildrenList(){
        return adminInfoService.getRoleAuthChildrenList();
    }

    @ApiOperation(value="通过roleID获取指定角色对应的权限子父级列表")
    @GetMapping("/getAuthChildrenListByRoleId/{roleId}")
    public DataResult getRoleAuthChildrenListByRoleId(@PathVariable Short roleId){
        return adminInfoService.getRoleAuthChildrenListByRoleId(roleId);
    }

    @ApiOperation(value="删除角色对应的权限")
    @DeleteMapping("/role/del/{roleId}/{authId}")
    public DataResult deleteRoleAuth(@PathVariable Short roleId,@PathVariable Short authId){
        return adminInfoService.deleteRoleAuth(roleId,authId);
    }
    @ApiOperation(value="更新角色菜单")
    @PostMapping("/role/update/{roleId}")
    public DataResult updateAuthMenu(@PathVariable Short roleId, Short[] authIds){
        return adminInfoService.updateAuthMenu(roleId,authIds);
    }
}
